﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;

namespace 渐变.common
{
    public class UniversalUtil
    {

        public static Image Base64ToImage(string base64str)
        {
            byte[] b = Convert.FromBase64String(base64str);
            MemoryStream ms = new MemoryStream(b);
            Image bitmap = new Bitmap(ms);
            return bitmap;
        }

        public static string ImageToBase64Str(Image img)
        {
            string strbaser64 = string.Empty;
            try
            {
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
            }
            catch (Exception)
            {
                throw new Exception("Something wrong during convert!");
            }
            return strbaser64;
        }
    }
}
